package com.bykj.taketurns.activity;

import android.animation.LayoutTransition;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.adapter.PersonalRecyAdapter;
import com.bykj.taketurns.model.PersonalBean;
import com.bykj.taketurns.utils.Constant;
import com.bykj.taketurns.widget.PersonalRecyclerview;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class PersonalActivity extends BaseActivity implements View.OnTouchListener {
    private LinearLayout mLineTab;
    private PersonalRecyclerview mRecyList;
    private TextView mTvNameMenu;
    private TextView mTvIdMenu;
    private TextView mTvSignatureMenu;
    private RelativeLayout mVideoAttentionRela;
    private ImageView mMenuTransmitImg;
    private ImageView mAppVideoFinish;
    private ImageView mVideoHeadImg;
    private ImageView mIvPortraitMenu;
    private TextView mTvPart;
    private TextView mTvAttention;
    private int widthPixels;
    private int heightPixels;
    private ArrayList<Fragment> slidingList=new ArrayList<>();
    private PersonalBean personalBean;
    private PersonalRecyAdapter takeAdapter;
    private TextView mVideoTvAdd;
    private boolean isAttentionStatus;
    private ImageView mVideoAddImg;
    private int mTouchSlop;
    private AppBarLayout mPersonMenuApp;
    private CoordinatorLayout coordPerson;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void initView(Bundle savedInstanceState) {
        ViewConfiguration configuration = ViewConfiguration.get(this);
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
        //参与
        mTvPart = (TextView) setViewById(R.id.menu_title_tv_part);
        //关注
        mTvAttention = (TextView) setViewById(R.id.menu_title_tv_attention);
        mLineTab = (LinearLayout) setViewById(R.id.line_menu_tab);//存放tab的容器
        //侧滑recyclerview
        mRecyList = (PersonalRecyclerview) setViewById(R.id.fl_control);
        //用户昵称
        mTvNameMenu = (TextView) setViewById(R.id.tv_name_menu);
        //粉丝数量
        mTvIdMenu = (TextView) setViewById(R.id.tv_id_menu);
        //关注数量
        mTvSignatureMenu = (TextView) setViewById(R.id.tv_signature_menu);
        //当前关注状态
        mVideoAttentionRela = (RelativeLayout) setViewById(R.id.video_attention_rela);
        //分享按钮
        mMenuTransmitImg = (ImageView) setViewById(R.id.menu_transmit_img);
        //返回按钮
        mAppVideoFinish = (ImageView) setViewById(R.id.app_video_finish);
        //用户关注头像
        mVideoHeadImg = (ImageView) setViewById(R.id.video_head_img);
        mVideoAddImg = (ImageView) setViewById(R.id.video_add_img);
        //关注
        mVideoTvAdd = (TextView) setViewById(R.id.video_tv_add);
        //用户头像
        mIvPortraitMenu = (ImageView) setViewById(R.id.iv_portrait_menu);
        mPersonMenuApp = (AppBarLayout) setViewById(R.id.person_menu_app);
        coordPerson = (CoordinatorLayout) setViewById(R.id.coord_person);
        //点击监听
        mTvPart.setOnClickListener(this);
        mTvAttention.setOnClickListener(this);
        //点击监听recyclerView的滑动
        //mRecyList.setOnTouchListener(this);
        mVideoAttentionRela.setOnClickListener(this);
        mVideoTvAdd.setOnClickListener(this);
        mVideoHeadImg.setOnClickListener(this);
        mVideoAddImg.setOnClickListener(this);
        mAppVideoFinish.setOnClickListener(this);
        //mPersonMenuApp.setOnTouchListener(this);
        coordPerson.setOnTouchListener(this);
        //默认展示第0个tab
        selectIndex(0);



    }

    @Override
    public void initData(Bundle savedInstanceState) {



    }

    @Override
    public int getParentView() {
        return R.layout.menu_personalcentre;
    }

    @Override
    public void initToolBar() {

    }

    @Override
    protected void postData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
           //点击监听参与
           case R.id.menu_title_tv_part:
                selectIndex(0);
                break;
           //点击监听关注
           case R.id.menu_title_tv_attention:
                selectIndex(1);
                break;
            case R.id.video_attention_rela:
            case  R.id.video_head_img:
            case  R.id.video_tv_add:
            case R.id.video_add_img:
                showMenu();
                break;
            case R.id.app_video_finish:
                finish();
                overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                break;

        }

    }
//    public void fullScreen(){
//        //右边侧换菜单显示全屏
//        DisplayMetrics metric = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(metric);
//        widthPixels = metric.widthPixels;
//        heightPixels = metric.heightPixels;
//        ViewGroup.LayoutParams leftParams = mRight.getLayoutParams();
//        leftParams.width=widthPixels;
//        leftParams.height=heightPixels;
//        mRight.setLayoutParams(leftParams);
//    }
    private void selectIndex(int index) {
        mTvPart.setSelected(false);
        mTvAttention.setSelected(false);
        getNetwork();
        LinearLayoutManager manager=new LinearLayoutManager(PersonalActivity.this);
        mRecyList.setLayoutManager(manager);
        switch (index) {
            case 0:
                mTvPart.setSelected(true);
                mTvPart.setBackgroundColor(Color.parseColor("#f23a58"));
                mTvAttention.setSelected(false);
                mTvAttention.setBackgroundColor(Color.parseColor("#000000"));
                //mRecyList.setAdapter(takeAdapter);
                break;
            case 1:
                mTvAttention.setSelected(true);
                mTvAttention.setBackgroundColor(Color.parseColor("#f23a58"));
                mTvPart.setSelected(false);
                mTvPart.setBackgroundColor(Color.parseColor("#000000"));
                ///showFragment(slidingList.get(1), R.id.fl_control);
                break;
        }
    }
    //请求网络数据
    protected  void getNetwork(){
        map.put("uhiddenid","1111");
        String url = Constant.JHC_SERVICE +
                Constant.ServiceConstant.PERSONAL_URL + Constant.SetUrl(map);

        OkGo.get(url)     // 请求方式和请求url
                .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        personalBean = gson.fromJson(s, PersonalBean.class);

                        mTvNameMenu.setText(personalBean.getUnickname());

                        mTvIdMenu.setText("粉丝" + personalBean.getFensi());
                        List<PersonalBean.PartakeListBean> partakeList =personalBean.getPartakeList();
                        if(takeAdapter!=null){
                            takeAdapter=null;
                        }
                        takeAdapter = new PersonalRecyAdapter(PersonalActivity.this);
                        takeAdapter.setData(partakeList);
                        mRecyList.setAdapter(takeAdapter);
                        Log.d("zzz",partakeList.toString());


                    }

                });
    }
    private float mInitialMotionX;
    private float mInitialMotionY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                final float x = event.getX();
                final float y = event.getY();
                mInitialMotionX = x;
                mInitialMotionY = y;
                Log.d("zzz","downper");

                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                final float upX = event.getX();
                final float upY= event.getY();
                final float dx = upX - mInitialMotionX;
                final float dy = upY - mInitialMotionY;
                Log.d("zzz","open");
                if(dx>mTouchSlop){
                    if(dx>Math.abs(dy)) {
                        finish();
                        overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                        return true;
                    }else {
                        return false;
                    }
                }


        }
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
                final float x = motionEvent.getX();
                final float y = motionEvent.getY();
                mInitialMotionX = x;
                mInitialMotionY = y;
                Log.d("zzz","downper");

                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                final float upX = motionEvent.getX();
                final float upY= motionEvent.getY();
                final float dx = upX - mInitialMotionX;
                final float dy = upY - mInitialMotionY;
                Log.d("zzz","open");
                if(Math.abs(dx)>Math.abs(dy)) {
                    finish();
                    overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                    Log.d("zzz","upper");
                    return true;
                }else {
                    return false;
                }

        }
        return false;
    }
    //退出页面
    @Override
    protected void onDestroy() {
        super.onDestroy();
        map.remove("uhiddenid");
        map=null;
        OkGo.getInstance().cancelTag(this);


    }
    /**
     * 设置主播是否被关注
     */
    public void setAttention(boolean status) {
        isAttentionStatus=status;

    }
    /**
     * 显示菜单设置
     */
    public void showMenu() {

        if(!isAttentionStatus){
            startPropertyAnim();
            isAttentionStatus=!isAttentionStatus;

        }else {
            mVideoTvAdd.setVisibility(View.GONE);
            mVideoAddImg.setVisibility(View.GONE);
            reduce();
            isAttentionStatus=!isAttentionStatus;
        }

    }
    /**
     * 更改主播当前状态
     */
    public boolean getAttentionStatus(){
        return isAttentionStatus;
    }
    // 动画实际执行
    public void startPropertyAnim() {
        mVideoTvAdd.setVisibility(View.GONE);
        mVideoAddImg.setVisibility(View.GONE);
    }
    // 动画实际执行
    public void reduce() {

        LayoutTransition transition = new LayoutTransition();
        transition.setAnimator(LayoutTransition.CHANGE_APPEARING,
                null);
        mVideoTvAdd.setVisibility(View.VISIBLE);
        mVideoAddImg.setVisibility(View.VISIBLE);
        mVideoAttentionRela.setLayoutTransition(transition);
    }
}
